package com.platform.sys.modular.syncdata.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.platform.core.exception.ServiceException;
import com.platform.core.factory.PageFactory;
import com.platform.core.pojo.page.PageResult;
import com.platform.core.util.PoiUtil;
import com.platform.sys.modular.syncdata.entity.SyncDataSource;
import com.platform.sys.modular.syncdata.enums.SyncDataSourceExceptionEnum;
import com.platform.sys.modular.syncdata.mapper.SyncDataSourceMapper;
import com.platform.sys.modular.syncdata.param.SyncDataSourceParam;
import com.platform.sys.modular.syncdata.service.SyncDataSourceService;

import java.util.List;

/**
 * 要操作其他库的元数据字段配置service接口实现类
 *
 * @author gyp
 * @date 2022-12-04 20:43:04
 */
@Service
public class SyncDataSourceServiceImpl extends ServiceImpl<SyncDataSourceMapper, SyncDataSource> implements SyncDataSourceService {

    @Override
    public PageResult<SyncDataSource> page(SyncDataSourceParam syncDataSourceParam) {
        QueryWrapper<SyncDataSource> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(syncDataSourceParam)) {

            // 根据标识 查询
            if (ObjectUtil.isNotEmpty(syncDataSourceParam.getCode())) {
                queryWrapper.lambda().eq(SyncDataSource::getCode, syncDataSourceParam.getCode());
            }
            // 根据要操作的字段 查询
            if (ObjectUtil.isNotEmpty(syncDataSourceParam.getOperationField())) {
                queryWrapper.lambda().eq(SyncDataSource::getOperationField, syncDataSourceParam.getOperationField());
            }
            // 根据要查询字段 查询
            if (ObjectUtil.isNotEmpty(syncDataSourceParam.getQueryField())) {
                queryWrapper.lambda().eq(SyncDataSource::getQueryField, syncDataSourceParam.getQueryField());
            }
            // 根据数据状态 查询
            if (ObjectUtil.isNotEmpty(syncDataSourceParam.getStatus())) {
                queryWrapper.lambda().eq(SyncDataSource::getStatus, syncDataSourceParam.getStatus());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<SyncDataSource> list(SyncDataSourceParam syncDataSourceParam) {

        QueryWrapper<SyncDataSource> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(syncDataSourceParam)) {

            // 根据标识 查询
            if (ObjectUtil.isNotEmpty(syncDataSourceParam.getCode())) {
                queryWrapper.lambda().eq(SyncDataSource::getCode, syncDataSourceParam.getCode());
            }
            // 根据要操作的字段 查询
            if (ObjectUtil.isNotEmpty(syncDataSourceParam.getOperationField())) {
                queryWrapper.lambda().eq(SyncDataSource::getOperationField, syncDataSourceParam.getOperationField());
            }
            // 根据要查询字段 查询
            if (ObjectUtil.isNotEmpty(syncDataSourceParam.getQueryField())) {
                queryWrapper.lambda().eq(SyncDataSource::getQueryField, syncDataSourceParam.getQueryField());
            }
            // 根据数据状态 查询
            if (ObjectUtil.isNotEmpty(syncDataSourceParam.getStatus())) {
                queryWrapper.lambda().eq(SyncDataSource::getStatus, syncDataSourceParam.getStatus());
            }
        }

        return this.list(queryWrapper);
    }

    @Override
    public void add(SyncDataSourceParam syncDataSourceParam) {
        SyncDataSource syncDataSource = new SyncDataSource();
        BeanUtil.copyProperties(syncDataSourceParam, syncDataSource);
        this.save(syncDataSource);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SyncDataSourceParam> syncDataSourceParamList) {
        syncDataSourceParamList.forEach(syncDataSourceParam -> {
            this.removeById(syncDataSourceParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SyncDataSourceParam syncDataSourceParam) {
        SyncDataSource syncDataSource = this.querySyncDataSource(syncDataSourceParam);
        BeanUtil.copyProperties(syncDataSourceParam, syncDataSource);
        this.updateById(syncDataSource);
    }

    @Override
    public SyncDataSource detail(SyncDataSourceParam syncDataSourceParam) {
        return this.querySyncDataSource(syncDataSourceParam);
    }

    /**
     * 获取要操作其他库的元数据字段配置
     *
     * @author gyp
     * @date 2022-12-04 20:43:04
     */
    private SyncDataSource querySyncDataSource(SyncDataSourceParam syncDataSourceParam) {
        SyncDataSource syncDataSource = this.getById(syncDataSourceParam.getId());
        if (ObjectUtil.isNull(syncDataSource)) {
            throw new ServiceException(SyncDataSourceExceptionEnum.NOT_EXIST);
        }
        return syncDataSource;
    }

    @Override
    public void export(SyncDataSourceParam syncDataSourceParam) {
        List<SyncDataSource> list = this.list(syncDataSourceParam);
        PoiUtil.exportExcelWithStream("SyncDataSource.xls", SyncDataSource.class, list);
    }

}
